package com.theanh.RssActivity.ui.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.theanh.RssActivity.entity.*;
import com.theanh.RssActivity.ui.model.CategoryModel;
import com.theanh.RssActivity.ui.model.ChannelModel;
import com.theanh.RssActivity.R;

public class ChannelAdapter extends BaseAdapter{
	public static final String TAG = "ChannelAdapter";
	public List<Channel> channels;
	private LayoutInflater inflater;
	
	public ChannelAdapter(Context context){
		inflater = LayoutInflater.from(context);
	}
	
	public ChannelAdapter(Context context, List<Channel> channels){
		inflater = LayoutInflater.from(context);
		this.channels = channels;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return channels == null ? 0 : channels.size();
	}

	@Override
	public Object getItem(int position) {
		return (channels != null && channels.size() > position) ? channels.get(position) : null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {

		//TODO: test
		ChannelModel model;
		
		if (view == null) {
			view = inflater.inflate(R.layout.row_channel, null);
			
			// Creates a ViewHolder and store references to the two children
			// views
			// we want to bind data to.
			model = ChannelModel.createModel(view);

			view.setTag(model);
		} else {
			// Get the ViewHolder back to get fast access to the TextView
			// and the ImageView.
			model = (ChannelModel) view.getTag();
		}
		
		// Bind the data efficiently with the holder.
		bind(model, position);

		return view;
	}
	
	private void bind(ChannelModel model, int position){
		if (channels == null || channels.size() < position - 1)
			return;
		
		Channel channel = channels.get(position);
		if (channel != null){
			model.bind(channel);
		}
	}

}
